package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.bean.SureOrderBean;
import com.avic.rong.utils.GlideUtils;
import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by XinEr on 2019/7/12.
 * Describe:
 */
public class MoreProductsListAdapter extends RecyclerView.Adapter<MoreProductsListAdapter.ViewHolder> {
    private Context context;
    private List<SureOrderBean.GoodsListBean> goodsList;
    private int moreOrderType;
    private String moreBiaoZhunImg;



    public MoreProductsListAdapter(Context context, List<SureOrderBean.GoodsListBean> goodsList) {
        this.context = context;
        this.goodsList = goodsList;
    }

    public String getMoreBiaoZhunImg() {
        return moreBiaoZhunImg;
    }

    public void setMoreBiaoZhunImg(String moreBiaoZhunImg) {
        this.moreBiaoZhunImg = moreBiaoZhunImg;
    }

    public int getMoreOrderType() {
        return moreOrderType;
    }

    public void setMoreOrderType(int moreOrderType) {
        this.moreOrderType = moreOrderType;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.more_products_list_item, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        SureOrderBean.GoodsListBean goodsListBean = goodsList.get(i);

        if (getMoreOrderType() == 4) {
            if (!TextUtils.isEmpty(goodsListBean.getGoodsImg())) {
                if (goodsListBean.getGoodsImg().contains("rongrong.")) {
                    GlideUtils.loaderImage(context, goodsListBean.getGoodsImg(), viewHolder.iv_goods_pic);
                } else {
                    GlideUtils.loaderImage(context, getMoreBiaoZhunImg() + goodsListBean.getGoodsImg(), viewHolder.iv_goods_pic);
                }
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.GONE);
            } else {
                String keywords = goodsListBean.getKeywords();
                String catName = goodsListBean.getCatName();
                viewHolder.tv_goods_type.setText(catName);
                viewHolder.tv_goods_tel.setText(keywords);
                viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_goods_pic);
            }
        } else {
            if (!TextUtils.isEmpty(goodsListBean.getGoodsImg())) {
                GlideUtils.loaderImage(context, goodsListBean.getGoodsImg(), viewHolder.iv_goods_pic);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.GONE);
            } else {
                //默认图
//                GlideUtils.loaderImage(context, R.mipmap.common_bg, iv_goods_pic);
                String keywords = goodsListBean.getKeywords();
                String catName = goodsListBean.getCatName();
                viewHolder.tv_goods_type.setText(catName);
                viewHolder.tv_goods_tel.setText(keywords);
                Glide.with(context).load(R.mipmap.common_bg).into(viewHolder.iv_goods_pic);
                viewHolder.iv_goods_pic.setVisibility(View.VISIBLE);
                viewHolder.ll_pic_tag.setVisibility(View.VISIBLE);
            }

        }
        if (!TextUtils.isEmpty(goodsListBean.getAttrShow())){
            viewHolder.tv_status.setText(goodsListBean.getAttrShow());
            viewHolder.tv_status.setVisibility(View.VISIBLE);
        }

//        GlideUtils.loaderImage(context,goodsListBean.getGoodsImg(),viewHolder.iv_goods_pic);
        viewHolder.tv_goods_title.setText(goodsListBean.getKeywords() + "  " + goodsListBean.getGoodsName());
        viewHolder.tv_goods_subtitle.setText(goodsListBean.getSkuName());
        viewHolder.tv_goods_number.setText("x" + goodsListBean.getGoodsNumber());
        viewHolder.tv_goods_price.setText("¥" + goodsListBean.getRealPrice());

    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_goods_pic;
        private TextView tv_goods_title;
        private TextView tv_goods_subtitle;
        private TextView tv_goods_price;
        private TextView tv_goods_number;

        private TextView tv_status;
        private TextView tv_goods_type;
        private TextView tv_goods_tel;
        private LinearLayout ll_pic_tag;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_goods_pic = itemView.findViewById(R.id.iv_goods_pic);
            tv_goods_title = itemView.findViewById(R.id.tv_goods_title);
            tv_goods_subtitle = itemView.findViewById(R.id.tv_goods_subtitle);
            tv_goods_price = itemView.findViewById(R.id.tv_goods_price);
            tv_goods_number = itemView.findViewById(R.id.tv_goods_number);
            tv_goods_type = itemView.findViewById(R.id.tv_goods_type);
            tv_goods_tel = itemView.findViewById(R.id.tv_goods_tel);
            ll_pic_tag = itemView.findViewById(R.id.ll_pic_tag);
            tv_status = itemView.findViewById(R.id.tv_status);
        }
    }
}
